package com.qire.manhua.controller;

import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.support.base.EventController;
import com.qire.manhua.R;
import com.qire.manhua.activity.AccountCancellationGuideActivity;
import com.qire.manhua.databinding.ActivityAccountCancellationGuideBinding;
import com.qire.manhua.model.enumeration.AccountBusinessPanelLayout;
import com.qire.manhua.model.viewModel.impl.AccountCancellationGuideViewModel;

import androidx.annotation.NonNull;

public class AccountCancellationGuideController extends EventController<AccountCancellationGuideActivity> {

    private AccountCancellationGuideViewModel viewModel;

    public AccountCancellationGuideController(@NonNull AccountCancellationGuideActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityAccountCancellationGuideBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountCancellationGuideController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(AccountCancellationGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }
    /**************************************************************************************************************/

    /** 退出按钮事件响应 */
    public void backBtnOnClick(View view) {
        owner.finish();
    }

    /** 申请注销按钮事件响应 */
    public void appliesForCancellationBtnOnClick(View view) {
        viewModel.checkBeforeCancellationAccount();
    }

    /** 发送手机号码验证 */
    public void sendPhoneVerifyBtnOnClick(View view) {
        AccountBusinessPanelLayout.CodeVerify.addNext(AccountBusinessPanelLayout.AccountCancellationNotice);
        viewModel.nextPage();
        viewModel.fetchVerificationCode();
    }

    /** 进入人工申诉渠道入口按钮 */
    public void artificialServiceBtnOnClick(View view) {
        AccountBusinessPanelLayout.AccountCancellationByAppeal.addNext(AccountBusinessPanelLayout.AccountCancellationWaiting);
        AccountBusinessPanelLayout.OldPhoneVerify.addNext(AccountBusinessPanelLayout.AccountCancellationByAppeal);
        viewModel.gotoPage(AccountBusinessPanelLayout.OldPhoneVerify);
    }

    /** 旧手机号码验证 */
    public void oldPhoneVerifyBtnOnClick(View view) {
        viewModel.authOldPhoneNumber();
    }

    /** 人工服务提交申诉信息 */
    public void submitAppealInfoBtnOnClick(View view) {
        viewModel.accountCancellationByArtificial();
    }

    /** 人工审核通过去注销 */
    public void gotoAccountCancellationBtnOnClick(View view) {
        viewModel.nextPage();
    }

    /** 获取验证码 */
    public void fetchVerifyCodeBtnOnClick(View view) {
        viewModel.fetchVerificationCode();
    }

    /** 验证码验证按钮事件响应 */
    public void verifyBtnOnClick(View view) {
        viewModel.checkVerificationCode();
    }

    /** 账户注销协议 */
    public void accountCancellationProtocolBtnOnClick(View view) {
        viewModel.toggleAgreeToDeal();
        if(viewModel.agreeToDeal()) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                    .withData("title", "账户注销协议")
                    .withData("url", Url.accountCancellationProtocol)
                    .navigation();
        }
    }

    public void verifyCodeOnFocusChange(View view, boolean hasFocus) {
        if(hasFocus) {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_n);
        } else {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_s);
        }
    }
    //解决光标在最前面的位置导致退格键失效的问题
    public boolean verifyCodeOnKey(View view, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_DEL) {
            EditText editText = (EditText) view;
            int textLength = editText.getText().toString().length();
            if(textLength > 0) {
                editText.setSelection(textLength);
            } else {
                editText.setText("");
            }
        }
        return false;
    }

}
